import { Raycaster, Vector2 } from 'three';
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js';
import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass.js';
import { OutputPass } from 'three/examples/jsm/postprocessing/OutputPass.js';
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';

export function initoutlinePassesAndComposer(option) {
  const { scene, camera, renderer, transformControls, objects } = option;
  const box = renderer.domElement;

  const raycaster = new Raycaster();
  // 后期处理
  const outlineComposer = new EffectComposer(renderer);
  const renderPass = new RenderPass(scene, camera);
  // renderPass.camera.layers.set(OUTLINE_SCENE);
  outlineComposer.addPass(renderPass);

  // 轮廓
  const outlinePass = new OutlinePass(new Vector2(box.clientWidth, box.clientHeight), scene, camera);
  outlinePass.visibleEdgeColor.set(0xff0000); // 设置可见边缘的颜色
  OutlinePass;
  outlineComposer.addPass(outlinePass);

  // 色彩校正
  const outputPass = new OutputPass();
  outlineComposer.addPass(outputPass);

  // FIXME: 需要准确设置 outlinePass.selectedObjects = [];
  return {
    outlineComposer,
    passes: [renderPass, outlinePass, outputPass],
  };
}

/**
 * @description 当某场景中的某个东西由多个mesh组成时,需要为其构建一个ancestors
 * 使用intersects[0].object.ancestors访问
 * @param {Array<import('three').Object3D>} meshs
 */
export function createClickAncestor(meshs) {
  if (Array.isArray(meshs)) {
    for (let i = 0; i < model.children.length; i++) {
      const group = model.children[i];
      group.traverse(function (obj) {
        if (obj.isMesh) {
          obj.ancestors = group;
        }
      });
    }
  }
}
